
var ws = require("nodejs-websocket");

console.log("开始建立连接...")
var userlist = {};

var server = ws.createServer(function(conn){

    var user = conn.path.split('/')[1];
    console.log('new connection successfully userId:'+user);
    userlist[user] = conn;

    conn.on("text", function (str) {
        var msg = JSON.parse(str);
        console.log(msg);
        if(userlist.hasOwnProperty(msg.fields.admin)){
          userlist[msg.fields.admin].sendText(str);
        }
    })
    conn.on("close", function (code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function (code, reason) {
        console.log("异常关闭")
    });
}).listen(9001)
console.log("WebSocket建立完毕")
